# -*- coding: utf-8 -*-
# created on 2016/10/31

from sympy import symbols
from mathsolver.functions.root.jiefangchenzu import JieFangChenZu
from mathsolver.functions.root.jiefangchen import JieFangChen
from mathsolver.functions.base.base import BaseFunction, new_latex
from mathsolver.functions.base.objects import BaseFunc, BaseEq, BaseEqs, BaseSymbolValue


class DaiDingXiShuQiuCan(BaseFunction):
    def solver(self, *args):
        """
        利用待定系数法对对数/指数/幂/一次/二次/反比例函数求参
        :param args: [0] - baseString "对数函数"/"指数函数"/"幂函数"/"一次函数"/"二次函数"/"反比例函数", [2] - BasePoint/BasePoints 点坐标
        :return: BaseEq/BaseEqs - 参数值
        """
        x, k = symbols('x k')
        if "二次函数" not in args[0]:
            f1, f2 = args[1].sympify()
        if "对数函数" in args[0]:
            self.steps.append(["设对数函数f(x)={}，且其图像经过({},{})".format(r"log_k{x}", new_latex(f1), new_latex(f2)), ""])
            func = BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'log(x,k)'})
        elif "指数函数" in args[0]:
            self.steps.append(["设指数函数f(x)={}，且其图像经过({},{})".format(r"k^{x}", new_latex(f1), new_latex(f2)), ""])
            func = BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'k**x'})
        elif "幂函数" in args[0]:
            self.steps.append(["设幂函数f(x)={}，且其图像经过({},{})".format(r"x^{k}", new_latex(f1), new_latex(f2)), ""])
            func = BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'x**k'})
            if f1 < 0 and f2 < 0:
                self.steps.append(["∵ 幂函数f(x)的图像经过({},{})".format(new_latex(f1), new_latex(f2)), "即该函数图像经过第三象限"])
                f1, f2 = -f1, -f2
                self.steps.append(["∴ 该幂函数必定为奇函数", "即该函数图像必定经过({},{})".format(new_latex(f1), new_latex(f2))])
        elif "一次函数" in args[0]:
            self.steps.append(["设一次函数f(x)={}，且其图像经过({},{})".format("kx", new_latex(f1), new_latex(f2)), ""])
            func = BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'k*x'})
        elif "反比例函数" in args[0]:
            self.steps.append(["设反比例函数f(x)={}，且其图像经过({},{})".format("k/x", new_latex(f1), new_latex(f2)), ""])
            func = BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'k/x'})
        elif "二次函数" in args[0]:
            # 三个点
            f = args[1].sympify()
            f1, f2, f3, f4, f5, f6 = f[0][0], f[0][1], f[1][0], f[1][1], f[2][0], f[2][1]
            self.steps.append(
                ["设二次函数f(x)={}，且其图像经过({},{}),({},{}),({},{})".format(r"ax^{2}+bx+c", new_latex(f1), new_latex(f2), new_latex(f3), new_latex(f4), new_latex(f5), new_latex(f6)), ""])
            func = BaseFunc({'name': 'f', 'var': 'x', 'type': '', 'expression': 'a*x**2+b*x+c'})
        else:
            raise NotImplementedError(args[0])
        self.steps.append(["代入该点坐标即可求出参数k的值", ""])
        if "对数函数" in args[0]:
            self.steps.append(["即{}={}".format(r"log_k{" + str(f1) + "}", f2), ""])
        elif "二次函数" in args[0]:
            eqs = BaseEqs(
                [[func.expression.subs(x, f1), f2], [func.expression.subs(x, f3), f4],
                 [func.expression.subs(x, f5), f6]])
            self.steps.append(["即" + eqs.printing(), ""])
        else:
            self.steps.append(["即{}={}".format(func.expression.subs(x, f1), f2), ""])
        if "二次函数" in args[0]:
            jiede = JieFangChenZu().solver(eqs)
        else:
            jiede = JieFangChen().solver(BaseEq([func.expression.subs(x, f1), f2]))
        for step in jiede.steps:
            self.steps.append([step[0], step[1]])
        output = jiede.output[0].value
        key = output.keys()[0]
        value = output[key]
        if isinstance(key, list):
            for i in range(len(key)):
                self.output.append(BaseSymbolValue({key[i]: value.args[0][i]}))
        else:
            self.output.append(BaseSymbolValue({key: value}))
        self.label.add("待定系数法求参-函数解析式")
        return self


if __name__ == '__main__':
    pass
